package juc.thread.threadPool;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.*;

/*
* ThreadPoolExecutor 线程池的方法使用
* */
public class Test5 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(1);
//        test1(executorService);
//          test2(executorService);
        //   test3(executorService);
        test4(executorService);
    }
    /*
    * 停止方法
    *
    *
    * */
    private static void test4(ExecutorService executorService)throws InterruptedException, ExecutionException{
        Future<?> future1 = executorService.submit(() -> {
            System.out.println("任务1...");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("任务1exd...");
        });
        Future<?> future2 = executorService.submit(() -> {
            System.out.println("任务2...");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("任务2exd...");
        });
        Future<?> future3 = executorService.submit(() -> {
            System.out.println("任务3...");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("任务3exd...");
        });
        /*System.out.println("shutdown...");
        executorService.shutdown();*/

       /* System.out.println("shutdown now...");
        List<Runnable> runnables = executorService.shutdownNow();*/

        System.out.println("awaitTermination...");
        System.out.println(executorService.awaitTermination(1, TimeUnit.SECONDS));
        System.out.println("...");
    }
    /*
    * invokeAny
    * */
    private static void test3(ExecutorService executorService)throws InterruptedException, ExecutionException{
        Object any = executorService.invokeAny(Arrays.asList(
                () -> {
                    System.out.println("任务1");
                    Thread.sleep(500);
                    return "1";
                },
                () -> {
                    System.out.println("任务2");
                    Thread.sleep(1500);
                    System.out.println("任务2e");
                    return "2";
                },
                () -> {
                    System.out.println("任务3");
                    Thread.sleep(2000);
                    return "3";
                }
        ));
        System.out.println(any);
    }
    /*
    * invokeAll
    * */
    private static void test2(ExecutorService executorService)throws InterruptedException, ExecutionException{

        List<Future<String>> futures = executorService.invokeAll(
                Arrays.asList(
                        ()->{
                            System.out.println("任务1");Thread.sleep(500);
                            return "1";
                        },
                        ()->{
                            System.out.println("任务2");Thread.sleep(1500);
                            return "2";
                        },
                        ()->{
                            System.out.println("任务3");Thread.sleep(2000);
                            return "3";
                        }
                        ));
        futures.forEach((r)->{
            String res = null;
            try {
                res = r.get();
            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
            }
            System.out.println(res);
        });
    }
/*submit
* */
    private static void test1(ExecutorService executorService) throws InterruptedException, ExecutionException {
        Future<String> future = executorService.submit(() -> {
            System.out.println("callable");
            Thread.sleep(1000);
            return "ok";
        });
        System.out.println("main...");
        String fResult = future.get();//原理 保护性暂停模式
        System.out.println(fResult);
    }
}

